<template>
	<scroll-view scroll-y="true" class="page">
		<u-navbar :placeholder="true" title="编辑心愿" @leftClick="$utils.back()" border>

		</u-navbar>
		<view>
			<view style="align-items: center;" class="  borderBottom flex pb20 pt20">
				<view class="mr20 ">
					心愿背景：
				</view>
				<view style="flex: 1;display: flex;justify-content: flex-end;">
					<view v-if="imageUrl.length==0" class=" upload flex" @click="openSelect">
						<u-icon name="camera-fill" color="#D3D4D6" size="50"></u-icon>

					</view>
					<view v-if="imageShow">
						<imageUpload type='radio' :imageShow='imageShow' @changeImageList='changeImageList'
							:imageUrl='imageUrl' @close='imageShow=null'  />
					</view>
				</view>
			</view>
			<view style="align-items: center;" class="mt20 borderBottom  flex pb20 pt20">
				<view class="mr20 ">
					积分：
				</view>

				<view style="flex: 1;">
					<u--input inputAlign='right' placeholder="请输入积分" border="none" v-model="point"
						type='number'></u--input>
				</view>
			</view>
			<view style="align-items: center;" class="mt20 borderBottom  flex pb20 pt20">
				<view class="mr20 ">
					标题：
				</view>
				<view style="flex: 1;display: flex;justify-content: flex-end;">
					<u--input inputAlign='right' placeholder="请输入标题" border="none" v-model="title"></u--input>
				</view>
			</view>
			<view style="align-items: center;" class="mt20 borderBottom  flex pb20 pt20">
				<view class="mr20 ">
					开始时间：
				</view>
				<view style="flex: 1;display: flex;justify-content: flex-end;" @click="calendarShow=true">
					{{start_at?start_at:'选择开始时间'}}
				</view>
			</view>
			<u-popup :show="showSelect" :round="10" @close="showSelect=false">
				<view class="popupTitle">
					<u-icon name="close" color='#fff'></u-icon>
					<view>
						选择方式
					</view>
					<u-icon name="close" @click="showSelect=false"></u-icon>
				</view>
				<view style="height: 20vh;">
					<u-grid :border="false" @click="clickGrid">
						<u-grid-item v-for="(baseListItem,baseListIndex) in baseList" :key="baseListIndex">
							<u-icon :customStyle="{paddingTop:20+'rpx'}" :name="baseListItem.name" :size="22"></u-icon>
							<text class="grid-text">{{baseListItem.title}}</text>
						</u-grid-item>
					</u-grid>
				</view>
			</u-popup>

		</view>
		<view class="bottom viewWidth">
			<u-button :disabled="disabled" @click="pulish">保存</u-button>
		</view>
		<root-portal>
			<u-calendar :minDate='minDate()' :show="calendarShow" @close='calendarShow=false'
				@confirm='confirm'></u-calendar>
		</root-portal>
	</scroll-view>
</template>

<script>
	import imageUpload from '@/components/imageUpload';
	export default {
		components: {
			imageUpload
		},
		data() {
			return {
				user: uni.getStorageSync('user'),
				calendarShow: false,
				start_at: null,
				point: 1,
				showSelect: false,
				imageShow: null,
				baseList: [{
						name: 'photo',
						title: '本地图片'
					},
					{
						name: 'chat',
						title: '素材聊天'
					},
				],
				imageUrl: [],
				title: null,
				disabled: false,
				loading: true,
				wish_id: null
			}
		},
		onLoad(op) {
			this.wish_id = op.id
			uni.showLoading({
				title: '加载中...'
			})
			this.$api.Wish.Info(op.id).then(res => {
				uni.hideLoading()
				const detail = res.data.wish
				this.title = detail.title
				this.point = detail.point
				this.start_at = detail.start_at
				this.imageShow=true
				this.imageUrl = [{
					url: detail.thumb,
					coverUrl: detail.thumb,
					small_url: detail.thumb_small,
					id: null,
					upload: true,
					title: ''
				}]
			})
		},

		methods: {
			confirm(e) {
				this.start_at = e[0]
				this.calendarShow = false
			},
			minDate() {
				// 创建一个表示当前时间的Date对象
				var today = new Date();

				// 使用getDate()方法获取当前日期，并加上1得到明天的日期
				var tomorrowDate = today.getDate();

				// 使用setDate()方法设置Date对象的日期为明天的日期
				today.setDate(tomorrowDate);

				// 获取明天的年份、月份和日期
				var tomorrowYear = today.getFullYear();
				var tomorrowMonth = today.getMonth() + 1; // 月份从0开始，所以要加1
				var tomorrowDay = today.getDate();

				return tomorrowYear + "-" + tomorrowMonth + "-" + tomorrowDay
			},
			openSelect() {
				this.showSelect = true
			},
			clickGrid(e) {
				if (e == 0) {
					this.imageShow = 'photo'
					this.showSelect = false
				} else {
					this.imageShow = 'chat'
					this.showSelect = false
				}
			},
			changeImageList(e) {
				this.imageUrl = e
			},
			pulish() {
				if (!this.imageUrl.every(item => item.upload) && this.imageUrl.length) {
					uni.$u.toast('正在上传中')
					return
				}

				if (!this.imageUrl.length) {
					uni.$u.toast('必须上传图片')
					return
				}

				if (!this.title) {
					uni.$u.toast('请输入标题')
					return
				}
				if (!this.start_at) {
					uni.$u.toast('必须选择开始时间')
					return
				}
				if (this.point <= 0) {
					uni.$u.toast('积分不能小于1')
					return
				}
			
				
				this.Add()

			},
			Add() {
				this.disabled = true
				let obj = {
					title: this.title,
					thumb: this.imageUrl[0].url,
					thumb_small: this.imageUrl[0].small_url,
					start_at: this.start_at,
					point: this.point
				}
				this.$api.Wish.Update(this.wish_id, obj).then(res => {
					uni.$u.toast('保存成功')
					setTimeout(() => {
						uni.reLaunch({
							url: '/My/wish'
						})
					}, 500)
				}).catch(err => {
					this.disabled = false
					console.log(err)
					uni.$u.toast(err.data.message)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		display: flex;
		height: calc(100vh - constant(safe-area-inset-bottom) + 10rpx);
		height: calc(100vh - env(safe-area-inset-bottom) + 10rpx);
		flex-direction: column;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.borderBottom {
		border-bottom: 1rpx solid #f1f1f1;
	}







	.flex {
		display: flex;
	}

	.pb20 {
		padding-bottom: 20rpx;
	}

	.pt20 {
		padding-top: 20rpx;
	}

	.upload {
		border-radius: 8rpx;
		background-color: #f5f5f5;
		width: 100rpx;
		height: 100rpx;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	::v-deep .u-border {
		border-color: #fff !important;
	}



	.selectItem {
		font-size: 30rpx;
		text-align: center;
		padding: 20rpx 0;
	}

	.selectItem:nth-child(1) {
		border-bottom: 1rpx solid #f1f1f1;
	}

	.userBox {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 20rpx 0;

		view:nth-child(2) {
			flex: 0.98;
		}
	}

	.bottom {
		margin-top: 20rpx;
		width: calc(100% - 40rpx);
	}

	.minus {
		width: 22px;
		height: 22px;
		border-width: 1px;
		border-color: #E6E6E6;
		border-style: solid;
		border-top-left-radius: 100px;
		border-top-right-radius: 100px;
		border-bottom-left-radius: 100px;
		border-bottom-right-radius: 100px;
		@include flex;
		justify-content: center;
		align-items: center;
	}

	.input {
		padding: 0 10px;
	}

	.plus {
		width: 22px;
		height: 22px;
		background-color: #FF0000;
		border-radius: 50%;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
	}

	.button {
		border-radius: 100rpx;
		border: 1rpx solid #bbb;
		padding: 5rpx 10rpx;
		font-size: 24rpx;
	}

	.rewardsBox {
		view {
			text-align: center;
		}

		>view {
			display: flex;
			padding: 10rpx 0;

			>view:nth-child(1) {
				justify-content: flex-start !important;
				padding: 0 10rpx;
				flex: 0.5;
			}

			>view:nth-child(2) {
				flex: 0.3;
			}

			>view:nth-child(3) {
				flex: 0.2;
			}
		}

		>view:nth-child(1) {
			background-color: #bbb;
			border-radius: 8rpx;
		}
	}
</style>